<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/5/25
 * Time: 17:10
 */

namespace App\Http\Controllers\Friend;


use App\Http\Classes\Friend;
use App\Http\Classes\IM\IM;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use App\Http\Classes\Log;

class FriendController extends BaseController
{
    public function addFriend(Request $request){
        try{
            if($request->has('fuid') && $request->has('group_id')){
                $data = array();
                $data['friend_user_id'] = $request->input('fuid');
                $data['friend_group_id'] = $request->input('group_id');
                $data['master_user_id'] = $_COOKIE['user_id'];
                if($data['friend_user_id'] != '' && $data['friend_group_id'] != ''){
                    if(Friend::isFriend($data)){
                        return $this->response(304,'已经好友了，请勿重复添加！',null);
                    }
                    else{
                        $friend = Friend::friendAdd($data);
                        if($friend === true){
                            $IM = new IM();
                            $dataArray = array();
                            $dataArray['accid'] = $data['master_user_id'];
                            $dataArray['faccid'] = $data['friend_user_id'];
                            $dataArray['type'] = 1;
                            $response = array();
                            $response['IM_response'] = json_decode($IM ->friendAdd($dataArray));
                            $response['result'] = true;
                            return $this->response(200,'添加成功！',$response);
                        }
                        elseif ($friend === null){
                            return $this->response(304,'添加失败!',null);
                        }
                        else{
                            return $this->response(503,'内部未知错误！',false);
                        }
                    }
                }
                else{
                    return $this->response(304,'参数不能为空！',null);
                }
            }
            else{
                return $this->response(503,'缺少必要参数！',null);
            }
        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }

    public function deleteFriend(Request $request){
        try{

        }
        catch (\Exception $exception){
            Log::userError(__FUNCTION__,$exception->getCode(),$exception->getMessage(),$exception->getFile(),$exception->getLine(),isset($_COOKIE['user_id']) ? $_COOKIE['user_id'] : 0);
            return $this->response(500,"系统内部错误！".$exception->getMessage(),null);
        }
    }
}